<?php
/**
 * 每周三榜单更新时给收藏/去购买 过该榜单中上榜单品的用户发push
 * 更新时间：每周三17:30
 */
ini_set('display_errors', 1);
ini_set('memory_limit', -1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\bigData\BrandRankWikiActionData;
use Biz\model\wiki\ObjRelateWiki as mObjRelateWiki;
use Biz\model\wiki\Wiki as mWiki;
use Biz\model\wiki\WikiBasic as mWikiBasic;
use Biz\model\wiki\WikiRankingHot;
use Biz\model\wiki\WikiRankingPublicPraise;
use Biz\service\wikiRanking\Ranking;
use Doraemon\pockets\constInterface\Push\PushId;
use Doraemon\service\Push\PushService;

//首次发最近半年的
//$startTime = '2020-09-01';
//$endTime = '2021-02-22';

//上一周
$startTime = date("Y-m-d", strtotime('monday last week'));
$endTime = date("Y-m-d", strtotime('monday this week'));

$pushedUids = [];

$rankingDay = Ranking::getRankingDay();
$where = [
    'ranking_day' => $rankingDay,
    'ranking_state' => WikiRankingHot::RANKING_STATE_SHOW
];
$hotList = WikiRankingHot::selectByWhere($where, ['basic_id', 'ranking_id']);
$hotBasicIds = array_column($hotList, 'basic_id');
$publicPraiseList = WikiRankingPublicPraise::selectByWhere($where, ['basic_id', 'ranking_id']);
$publicPraiseBasicIds = array_column($publicPraiseList, 'basic_id');
$basicIds = array_merge($hotBasicIds, $publicPraiseBasicIds);
$basicIds = array_unique($basicIds);
$wikiList = mWiki::selectByWhere(['basic_id' => $basicIds, 'status[!]' => mWiki::STATUS_DEL], ['id', 'basic_id']);
$basicList = mWikiBasic::selectByWhere(['id' => $basicIds], ['id', 'title']);
$basicList = array_column($basicList, null, 'id');
$basicWikiArr = [];
$wikiIds = [];
foreach ($wikiList as $row) {
    $basicWikiArr[$row['basic_id']][] = $row['id'];
    $wikiIds[] = $row['id'];
}

$rankingWikiList = [];
foreach ($hotList as $row) {
    $basicId = $row['basic_id'];
    $title = isset($basicList[$basicId]) ? $basicList[$basicId]['title'] : '';
    if (!isset($rankingWikiList[$row['ranking_id']][$basicId])) {
        $rankingWikiList[$row['ranking_id']][$basicId] = ['title' => $title, 'ids' => []];
    }
    if (isset($basicWikiArr[$basicId])) {
        $ids = $basicWikiArr[$basicId];
        $arr = [
            'title' => $title,
            'ids' => $ids
        ];
        $rankingWikiList[$row['ranking_id']][$basicId] = array_merge($rankingWikiList[$row['ranking_id']][$basicId], $arr);
    }
}

foreach ($publicPraiseList as $row) {
    $basicId = $row['basic_id'];
    $title = isset($basicList[$basicId]) ? $basicList[$basicId]['title'] : '';
    if (!isset($rankingWikiList[$row['ranking_id']][$basicId])) {
        $rankingWikiList[$row['ranking_id']][$basicId] = ['title' => $title, 'ids' => []];
    }
    if (isset($basicWikiArr[$basicId])) {
        $ids = $basicWikiArr[$basicId];
        $arr = [
            'title' => $title,
            'ids' => $ids
        ];
        $rankingWikiList[$row['ranking_id']][$basicId] = array_merge($rankingWikiList[$row['ranking_id']][$basicId], $arr);
    }
}


//查询过去一周用户关联的wiki在本期上榜，发送push消息
$relateList = mObjRelateWiki::getByWhere(['wiki_id' => $wikiIds, 'addtime[>=]' => $startTime, 'addtime[<]' => $endTime], ['uid', 'wiki_id']);
$relateWikiUids = [];
foreach ($relateList as $row) {
    $relateWikiUids[$row['wiki_id']][] = $row['uid'];
}

foreach ($rankingWikiList as $rankingId => $rows) {
    foreach ($rows as $basicId => $row) {
        $title = $row['title'];
        if ($title) {
            foreach ($row['ids'] as $wikiId) {
                if (isset($relateWikiUids[$wikiId])) {
                    $uids = $relateWikiUids[$wikiId];
                    foreach ($uids as $uid) {
                        if (!in_array($uid, $pushedUids)) {
                            $pushedUids[] = $uid;
                            $desc = '太厉害了！你推荐的' . $title . '上榜啦！快来看看 >>';
                            $link = Ranking::getRankingUrl($rankingId, 1);
                            pushMsg($uid, '本周好物榜TOP10', $desc, $link);
                        }
                    }
                }
            }
        }
    }
}

//查询过去一周用户收藏购买的wiki在本期上榜，发送push消息
$startDay = date('Ymd', strtotime($startTime));
$endDay = date('Ymd', strtotime($endTime));
$actionWikiUids = [];
$table = BrandRankWikiActionData::getTableName();
$sql = "SELECT `uid`,`wiki_id`,`day`, max(`ts`) as max_ts FROM {$table} where `day` >= {$startDay} and `day`< {$endDay} and `uid`>0 group by `uid`";
$actionList = BrandRankWikiActionData::query($sql)->fetchAll();

foreach ($actionList as $row) {
    $actionWikiUids[$row['wiki_id']][] = $row['uid'];
}

foreach ($rankingWikiList as $rankingId => $rows) {
    foreach ($rows as $basicId => $row) {
        $title = $row['title'];
        if ($title) {
            foreach ($row['ids'] as $wikiId) {
                if (isset($actionWikiUids[$wikiId])) {
                    $uids = $actionWikiUids[$wikiId];
                    foreach ($uids as $uid) {
                        if (!in_array($uid, $pushedUids)) {
                            $pushedUids[] = $uid;
                            $desc = '你喜爱的' . $title . '上榜啦！快来看看更多值得买好物 >>';
                            $link = Ranking::getRankingUrl($rankingId, 1);
                            pushMsg($uid, '本周好物榜TOP10', $desc, $link);
                        }
                    }
                }
            }
        }
    }
}

var_dump('已发送push的用户数：' . count($pushedUids));

function pushMsg($uid, $title, $desc, $link)
{
    $pushId = PushId::BRAND_RANK_WIKI_ACTION . date('Y-m-d');
    $params = [
        'uid' => $uid,
        'title' => $title,
        'alert' => $desc,
        'param' => [
            'link' => $link,
            'push_id' => $pushId,
            'image' => ''
        ]
    ];
    PushService::send($params);
}